package at.codejedi.sto.gui.combat;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import at.codejedi.sto.gui.detail.chart.BattleBarChartData;
import at.codejedi.sto.gui.detail.chart.BattleCompareChart;
import at.codejedi.sto.gui.lazy.LazyPanel;
import at.codejedi.sto.model.combat.IBattleStatistic;

public abstract class BattleStatisticsCompareView<TBattleStatistics extends IBattleStatistic>
		extends LazyPanel implements IDetailView {

	private static final long serialVersionUID = -2410921863605686309L;

	BattleCompareChart netAllChart;
	BattleCompareChart netDPSChart;
	BattleCompareChart rawAllChart;
	BattleCompareChart rawDPSChart;

	public BattleStatisticsCompareView() {
		netDPSChart = new BattleCompareChart("Net DPS Battles");
		rawDPSChart = new BattleCompareChart("Raw DPS Battles");
		netAllChart = new BattleCompareChart("Net All Damage Battles");
		rawAllChart = new BattleCompareChart("Raw All Damage Battles");
		setLayout(new GridLayout(2, 2, 0, 0));
		add(netDPSChart);
		add(rawDPSChart);
		add(netAllChart);
		add(rawAllChart);
	}

	public void display(Iterable<TBattleStatistics> statistics) {
		List<BattleBarChartData> netDPSChartData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> rawDPSChartData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> netAllChartData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> rawAllChartData = new ArrayList<BattleBarChartData>();

		boolean useCombindedName = false;
		List<String> names = new ArrayList<String>();
		for (TBattleStatistics statistic : statistics) {
			String name = getName(statistic);
			if(names.contains(name))
			{
				useCombindedName = true;
				break;
			}
			names.add(name);
		}

		for (TBattleStatistics statistic : statistics) {

			BattleBarChartData p1NetDPS = new BattleBarChartData();
			String name = getName(statistic,useCombindedName);
			p1NetDPS.name = name;
			p1NetDPS.dealt = statistic.getNetDamageDealt().getDamagePerSecond();
			p1NetDPS.received = statistic.getNetDamageReceived()
					.getDamagePerSecond();
			netDPSChartData.add(p1NetDPS);

			BattleBarChartData p1RawDPS = new BattleBarChartData();
			p1RawDPS.name = name;
			p1RawDPS.dealt = statistic.getRawDamageDealt().getDamagePerSecond();
			p1RawDPS.received = statistic.getRawDamageReceived()
					.getDamagePerSecond();
			rawDPSChartData.add(p1RawDPS);

			BattleBarChartData netAllData = new BattleBarChartData();
			netAllData.name = name;
			netAllData.dealt = statistic.getNetDamageDealt().getAllDamage();
			netAllData.received = statistic.getNetDamageReceived()
					.getAllDamage();
			netAllChartData.add(netAllData);

			BattleBarChartData rawAllData = new BattleBarChartData();
			rawAllData.name = name;
			rawAllData.dealt = statistic.getRawDamageDealt().getAllDamage();
			rawAllData.received = statistic.getRawDamageReceived()
					.getAllDamage();
			rawAllChartData.add(rawAllData);

		}

		netDPSChart.init(netDPSChartData);
		rawDPSChart.init(rawDPSChartData);
		netAllChart.init(netAllChartData);
		rawAllChart.init(rawAllChartData);

		rawAllChart.setVisible(false);
		rawAllChart.setVisible(true);
	}

	protected String getName(TBattleStatistics statistics){
		return getName(statistics,false);
	}
	protected abstract String getName(TBattleStatistics statistics, boolean uniqueName);

}
